package cisdi.mes.sm.rm.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cisdi.mes.sm.rm.dao.MeasureDocLogDao;
import cisdi.mes.sm.rm.dao.StorageContentDao;
import cisdi.mes.sm.rm.dao.StorageDao;
import cisdi.mes.sm.rm.domain.SmRmStorageContent;
import cisdi.mes.sm.rm.service.MatLoadMgr;
import cisdi.mes.sm.util.dao.IPcsDao;

@Service
public class MatLoadMgrImpl implements MatLoadMgr {

	@Autowired
	private StorageDao storageDao;
	@Autowired
	private StorageContentDao storageContentDao;
	@Autowired
	private MeasureDocLogDao measureDocLogDao;
	@Autowired
	private IPcsDao pcsDao;

	public StorageDao getStorageDao() {
		return storageDao;
	}

	public void setStorageDao(StorageDao storageDao) {
		this.storageDao = storageDao;
	}

	public IPcsDao getPcsDao() {
		return pcsDao;
	}

	public void setPcsDao(IPcsDao pcsDao) {
		this.pcsDao = pcsDao;
	}

	public MeasureDocLogDao getMeasureDocLogDao() {
		return measureDocLogDao;
	}

	public void setMeasureDocLogDao(MeasureDocLogDao measureDocLogDao) {
		this.measureDocLogDao = measureDocLogDao;
	}

	public StorageContentDao getStorageContentDao() {
		return storageContentDao;
	}

	public void setStorageContentDao(StorageContentDao storageContentDao) {
		this.storageContentDao = storageContentDao;
	}

	@Transactional
	@Override
	public Map<String, Object> loadBin(String from, String to, Double weight,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		String validString = validLoadBin(from, weight);
		if (validString.equals("success")) {
			
			int i = 1; //控制只发送一次料仓物料信息给PCS
			
			List<SmRmStorageContent> contents = storageContentDao.getContents(from);
			for (SmRmStorageContent content : contents) {
				if (weight.doubleValue() >= content.getWeight().doubleValue()) {
					storageContentDao.deleteContent(content);
					storageContentDao.addContent(to, content);
					measureDocLogDao.logMatLoad(to, content,  request);
					weight = weight - content.getWeight();
				} else {
					storageContentDao.decreaseWgt(content, weight);
					storageContentDao.addContent(to, content, weight);
					measureDocLogDao.logMatLoad(to, content, weight,  request);
					weight = 0d;
				}
				if (i == 1) {
					pcsDao.sendBin(to, content);
				}
				i = i + 1;
				
				pcsDao.sendAna(to, content);

				if (weight.doubleValue() <= 0) {
					break;
				}
			}
			map.put("success", true);
			map.put("message", "高位料仓上料成功");
		} else {
			map.put("success", false);
			map.put("message", validString);
		}

		return map;
	}


	@Override
	public Map<String, Object> loadManual(String from, String to, Double weight,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		String validString = validLoadBin(from, weight);
		if (validString.equals("success")) {
			
			List<SmRmStorageContent> contents = storageContentDao.getContents(from);
			for (SmRmStorageContent content : contents) {
				if (weight.doubleValue() >= content.getWeight().doubleValue()) {
					storageContentDao.deleteContent(content);
					storageContentDao.addContent(to, content);
					measureDocLogDao.logMatLoad(to, content,  request);
					weight = weight - content.getWeight();
				} else {
					storageContentDao.decreaseWgt(content, weight);
					storageContentDao.addContent(to, content, weight);
					measureDocLogDao.logMatLoad(to, content, weight,  request);
					weight = 0d;
				}
				if (weight.doubleValue() <= 0) {
					break;
				}
			}
			map.put("success", true);
			map.put("message", "高位料仓上料成功");
		} else {
			map.put("success", false);
			map.put("message", validString);
		}

		return map;
	}

	@Override
	public Map<String, Object> undoMove(long contentId,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String validString = validUndoMove(contentId);
		if (validString.equals("success")) {
			SmRmStorageContent bContent = storageContentDao.getById(contentId);
			SmRmStorageContent uContent = storageContentDao.getById(bContent.getFromContentId());
			if (uContent != null) {
				// 1a.增加低位料仓库存
				storageContentDao.backContent(uContent, bContent.getWeight());
			} else {
				// 1b.新增低位料仓库存
				storageContentDao.backContent(bContent);
			}
			storageContentDao.deleteContent(bContent);
			// 3.记录日志
			measureDocLogDao.logUndoLoadBin(bContent,request);
			map.put("success", true);
			map.put("message", "退料成功");
		} else {
			map.put("success", false);
			map.put("message", validString);
		}
		return map;
	}


	private String validLoadBin(String storageId, Double weight) {
		String validString = "";

		Boolean isWgtEnough = false;
		Double totalWgt = storageContentDao.getStorageWgt(storageId);
		if (totalWgt.doubleValue() >= weight.doubleValue()) {
			isWgtEnough = true;
		} else {
			validString += "上料重量大于库存现有量<br>";
		}

		if (isWgtEnough) {
			validString = "success";
		}

		return validString;
	}

	private String validUndoMove(long contentId) {
		String validString = "";

		Boolean isExist = false;
		SmRmStorageContent content = storageContentDao.getById(contentId);
		if (content != null) {
			isExist = true;
		} else {
			validString += "不存在该库存，请刷新页面";
		}

		Boolean isTracable = false;
		if (isExist && content.getFromContentId() != null) {
			isTracable = true;
		} else {
			validString += "该库存为直上料仓库存无法追溯到源料仓";
		}

		if (isExist && isTracable ) {
			validString = "success";
		}

		return validString;
	}

}
